/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.manage.core.domain.Position;
import info.peigen.hotpot.manage.core.repository.entity.PositionEntity;
import info.peigen.hotpot.manage.core.repository.mapper.PositionMapper;
import info.peigen.hotpot.manage.core.util.convertor.PositionConvertor;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(PositionRepository)</b>
 * "职位" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-28
 */
@SuppressWarnings("unused")
@Component
public class PositionRepository extends AbstractBeetlSqlRepository<PositionMapper, PositionEntity> {
    public Position active(String positionId) {
        Assert.isTrue(StrUtil.isNotEmpty(positionId), () -> new BusinessException(ResultCode.FAILURE, "激活时[positionId]不得为空!"));
        PositionEntity entity = getMapper().findByPositionId(positionId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!positionId=" + positionId));

        return PositionConvertor.INSTANCE.convertToDomain(entity);
    }

    public List<Position> active(List<String> positionIds) {
        Assert.isTrue(CollUtil.isNotEmpty(positionIds), () -> new BusinessException(ResultCode.FAILURE, "激活时[positionIds]不得为空!"));
        List<PositionEntity> entities = getMapper().listByPositionIds(positionIds);
        Assert.notNull(entities, () -> new BusinessException(ResultCode.FAILURE, "激活失败!positionId=" + positionIds));

        return PositionConvertor.INSTANCE.convertToDomainsByEntity(entities);

    }

    public Position lock(String positionId) {
        Assert.isTrue(StrUtil.isNotEmpty(positionId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[positionId]不得为空!"));
        PositionEntity entity = getMapper().lockByPositionId(positionId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!positionId为空!"));
        return PositionConvertor.INSTANCE.convertToDomain(entity);
    }

    public void removeByPositionId(String positionId) {
        Assert.isTrue(StrUtil.isNotEmpty(positionId), () -> new BusinessException(ResultCode.FAILURE, "删除时[positionId]不得为空!"));
        getMapper().deleteByPositionId(positionId);
    }

    public void logicDeleteById(Integer id){
        delete(id);
    }

    public int logicDeleteByPositionId(String positionId) {
        Assert.isTrue(StrUtil.isNotEmpty(positionId), () -> new BusinessException(ResultCode.FAILURE, "逻辑删除时[positionId]不得为空!"));
        return getMapper().createLambdaQuery().andEq(PositionEntity::getPositionId, positionId).delete();
    }

    public void store(Position domain){
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));
        PositionEntity entity = PositionConvertor.INSTANCE.convertToEntity(domain);
        saveOrUpdate(entity);
    }

}